home *** CD-ROM | disk | FTP | other *** search
/ CD Concept 6 / CD Concept 06.iso / mac / UTILITAIRE / Little Smalltalk v3.1.4 / C Source / Sources / filein.c < prev    next >
Text File  |  1994-11-01  |  5KB  |  208 lines

  1. /*
  2.     Little Smalltalk, version 3
  3.     Written by Tim Budd, Oregon State University, June 1988
  4.  
  5.     routines used in reading in textual descriptions of classes
  6. */
  7.  
  8. # include "env.h"
  9. # include "memory.h"
  10. # include "names.h"
  11. # include "lex.h"
  12.  
  13. #ifdef TCL
  14. # include "macio.h"
  15. # define fgets    mac_fgets        /* Use Mac-specific fgets () */
  16. #else
  17. # include <stdio.h>
  18. #endif
  19.  
  20. # ifdef STRING
  21. # include <string.h>
  22. # endif
  23. # ifdef STRINGS
  24. # include <strings.h>
  25. # endif
  26.  
  27. # define MethodTableSize 39
  28.  
  29. /*
  30.     the following are switch settings, with default values
  31. */
  32. boolean savetext = true;
  33.  
  34. /*
  35.     we read the input a line at a time, putting lines into the following
  36.     buffer.  In addition, all methods must also fit into this buffer.
  37. */
  38. # define TextBufferSize 1024
  39.  
  40. #ifdef THINKC
  41. # include "filein.proto.h"
  42. # include "memory.proto.h"
  43. # include "parser.proto.h"
  44. # include "lex.proto.h"
  45. # include "news.proto.h"
  46. # include "names.proto.h"
  47. # ifdef TCL
  48. #   include "tclprim.proto.h"
  49. # else
  50. #   include "winprim.proto.h"
  51. # endif
  52. #else
  53. static object findClass(char *name);
  54. static void readClassDeclaration(void);
  55. static readMethods(FILE *fd, boolean printit);
  56.  
  57. static object findClass(char *name);
  58. static readClassDeclaration(void);
  59. static readMethods(FILE *fd, boolean printit);
  60.  
  61. static object findClass(char *);
  62. static readClassDeclaration(void);
  63. static readMethods(FILE *, boolean);
  64. static object findClass(char *);
  65. static void readClassDeclaration(void);
  66. static readMethods(FILE *, boolean);
  67. static object findClass(char *name)
  68.  
  69. static char textBuffer [TextBufferSize];
  70. #endif
  71.  
  72. char textBuffer [TextBufferSize];
  73.  
  74. /*
  75.     findClass gets a class object,
  76.     either by finding it already or making it
  77.     in addition, it makes sure it has a size, by setting
  78.     the size to zero if it is nil.
  79. */
  80. object findClass(char *name)
  81. {    
  82.     object newobj;
  83.  
  84.     newobj = globalSymbol(name);
  85.     if (newobj == nilobj)
  86.         newobj = newClass(name);
  87.     if (basicAt(newobj, sizeInClass) == nilobj) {
  88.         basicAtPut(newobj, sizeInClass, newInteger(0));
  89.         }
  90.     return newobj;
  91. }
  92.  
  93. /*
  94.     readDeclaration reads a declaration of a class
  95. */
  96. static void readClassDeclaration(void)
  97. {    
  98.     object classObj, super, vars;
  99.     int i, size, instanceTop;
  100.     object instanceVariables[15];
  101.  
  102.     if (nextToken() != nameconst)
  103.         sysError("bad file format","no name in declaration");
  104.     classObj = findClass(tokenString);
  105.     size = 0;
  106.     if (nextToken() == nameconst) {    /* read superclass name */
  107.         super = findClass(tokenString);
  108.         basicAtPut(classObj, superClassInClass, super);
  109.         size = intValue(basicAt(super, sizeInClass));
  110.         ignore nextToken();
  111.         }
  112.     if (token == nameconst) {        /* read instance var names */
  113.         instanceTop = 0;
  114.         while (token == nameconst) {
  115.             instanceVariables[instanceTop++] = newSymbol(tokenString);
  116.             size++;
  117.             ignore nextToken();
  118.             }
  119.         vars = newArray(instanceTop);
  120.         for (i = 0; i < instanceTop; i++) {
  121.             basicAtPut(vars, i+1, instanceVariables[i]);
  122.             }
  123.         basicAtPut(classObj, variablesInClass, vars);
  124.         }
  125.     basicAtPut(classObj, sizeInClass, newInteger(size));
  126. }
  127.  
  128. /*
  129.     readClass reads a class method description
  130. */
  131. static void readMethods (FILE *fd, boolean printit)
  132. {    
  133.     object classObj, methTable, theMethod, selector;
  134. # define LINEBUFFERSIZE 512
  135.     char *cp, *eoftest, lineBuffer [LINEBUFFERSIZE];
  136.  
  137.     if (nextToken() != nameconst)
  138.         sysError("missing name","following Method keyword");
  139.     classObj = findClass(tokenString);
  140.     setInstanceVariables(classObj);
  141.     if (printit)
  142.         cp = charPtr(basicAt(classObj, nameInClass));
  143.  
  144.     /* now find or create a method table */
  145.     methTable = basicAt(classObj, methodsInClass);
  146.     if (methTable == nilobj) {    /* must make */
  147.         methTable = newDictionary(MethodTableSize);
  148.         basicAtPut(classObj, methodsInClass, methTable);
  149.         }
  150.  
  151.     /* now go read the methods */
  152.     do {
  153.         if (lineBuffer[0] == '|')    /* get any left over text */
  154.             strcpy(textBuffer,&lineBuffer[1]);
  155.         else
  156.             textBuffer[0] = '\0';        
  157.         while ( (eoftest
  158.                 = fgets (lineBuffer, LINEBUFFERSIZE, fd)) != NULL) {
  159.             if ((lineBuffer[0] == '|') || (lineBuffer[0] == ']'))
  160.                 break;
  161.             ignore strcat(textBuffer, lineBuffer);
  162.             }
  163.         if (eoftest == NULL) {
  164.             sysError("unexpected end of file","while reading method");
  165.             break;
  166.             }
  167.  
  168.         /* now we have a method */
  169.         theMethod = newMethod();
  170.         if (parse(theMethod, textBuffer, savetext)) {
  171.             selector = basicAt(theMethod, messageInMethod);
  172.             basicAtPut(theMethod, methodClassInMethod, classObj);
  173.             if (printit)
  174.                 dspMethod(cp, charPtr(selector));
  175.             nameTableInsert(methTable, (int) selector, 
  176.                 selector, theMethod);
  177.             }
  178.         else {
  179.             /* get rid of unwanted method */
  180.             incr(theMethod);
  181.             decr(theMethod);
  182.             givepause();
  183.             }
  184.         
  185.     } while (lineBuffer[0] != ']');
  186. }
  187.  
  188. /*
  189.     fileIn reads in a module definition
  190. */
  191. void fileIn(FILE *fd, boolean printit)
  192. {
  193.     while (fgets (textBuffer, TextBufferSize, fd) != NULL) {
  194.         lexinit(textBuffer);
  195.         if (token == inputend)
  196.             ; /* do nothing, get next line */
  197.         else if ((token == binary) && streq(tokenString, "*"))
  198.             ; /* do nothing, its a comment */
  199.         else if ((token == nameconst) && streq(tokenString, "Class"))
  200.             readClassDeclaration();
  201.         else if ((token == nameconst) && streq(tokenString,"Methods"))
  202.             readMethods(fd, printit);
  203.         else 
  204.             sysError("unrecognized line", textBuffer);
  205.         }
  206. }
  207.  
  208.